﻿namespace Sidvall.Net
{
    public class HttpSystemContext
    {
        #region Public Members

        #region PreAuthenticate

        private bool? _PreAuthenticate;
        public bool? PreAuthenticate
        {
            get
            {
                if (this.Proxy != null)
                    return this.Proxy.PreAuthenticate;
                return _PreAuthenticate;
            }
            set
            {
                _PreAuthenticate = value;
            }
        }

        #endregion
        #region Proxy

        private ProxySettings _Proxy;
        public ProxySettings Proxy
        {
            get
            {
                return _Proxy;
            }
            set
            {
                _Proxy = value;
            }
        }

        #endregion
        #region UseDefaultCredentials

        private bool? _UseDefaultCredentials;
        public bool? UseDefaultCredentials
        {
            get
            {
                if (this.Proxy != null)
                    return this.Proxy.UseDefaultCredentials;
                return _UseDefaultCredentials;
            }
            set
            {
                _UseDefaultCredentials = value;
            }
        }

        #endregion

        #region GetDefaultProxy

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public ProxySettings GetDefaultProxy()
        {
            ProxySettings proxy;

            if (this.Proxy != null)
            {
                proxy = this.Proxy.Copy();
            }
            else
            {
                proxy = new ProxySettings()
                {
                    UseDefaultCredentials = false,
                };
            }
            if (proxy.Credentials == null)
                proxy.Credentials = new CredentialsSettings();
            return proxy;
        }

        #endregion
        #region InitializeProxy

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public void InitializeProxy(ProxySettings proxy)
        {
            if (string.IsNullOrWhiteSpace(proxy?.Address))
            {
                this.Proxy = null;
            }
            else
            {
                this.Proxy = new ProxySettings()
                {
                    Address = proxy.Address,
                    PreAuthenticate = proxy.PreAuthenticate,
                    UseDefaultCredentials = proxy.UseDefaultCredentials,
                };
                if (!string.IsNullOrWhiteSpace(proxy?.Credentials?.UserName))
                {
                    this.Proxy.Credentials = new CredentialsSettings()
                    {
                        Domain = proxy.Credentials.Domain,
                        Password = proxy.Credentials.Password,
                        UserName = proxy.Credentials.UserName,
                    };
                }
            }
        }

        #endregion

        #endregion
        #region Factory Members

        #region Current

        private static readonly object _Mutex = new object();
        private static volatile HttpSystemContext _Current;
        public static HttpSystemContext Current
        {
            get
            {
                HttpSystemContext item;

                if (_Current == null)
                {
                    lock (_Mutex)
                    {
                        if (_Current == null)
                        {
                            item = new HttpSystemContext()
                            {
                            };
                            _Current = item;
                        }
                    }
                }
                return _Current;
            }
            set
            {
                lock (_Mutex)
                {
                    _Current = value;
                }
            }
        }

        #endregion

        #endregion
        #region Constructors

        public HttpSystemContext()
        {
        }

        #endregion
    }
}
